package com.woniuxy.lrz.carlproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.levelform.UpdateSecondLevelForm;
import com.woniuxy.lrz.carlproject.model.ServiceFirstLevel;
import com.woniuxy.lrz.carlproject.model.ServiceSecondLevel;
import com.woniuxy.lrz.carlproject.service.ServiceFirstLevelService;
import com.woniuxy.lrz.carlproject.service.ServiceSecondLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 二级分类服务表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/carlproject/serviceSecondLevel")
@Api("二级服务接口")
@Slf4j
public class ServiceSecondLevelController {

    @Resource
    private ServiceSecondLevelService serviceSecondLevelService;
    @Resource
    private ServiceFirstLevelService serviceFirstLevelService;

    @GetMapping("/getSecondLevel")
    @Log("查看所有二级服务接口")
    @ApiOperation("查询二级服务")
    public Result getSecondLevel(@RequestParam(defaultValue = "1")int current, @RequestParam(defaultValue = "10")  int size){
        Page<ServiceSecondLevel> all = serviceSecondLevelService.getAll(current, size);
        return  Result.success(all);
    }

    @GetMapping("/addSecondLevel")
    @Log("添加二级服务")
    @ApiOperation("添加二级服务")
    @ApiImplicitParam("服务名,一级服务id")
    public  Result addSecondLevel(String name,Integer serviceFirstId){
        log.info("二级服务名:{}一级服务id{}",name,serviceFirstId);
        //前端渲染出来的一级接口选择 不存在没有的一级服务id
        if (name==null || name.equals("")|| serviceFirstId==null || serviceFirstId<1){
            throw  new AppException("参数错误");
        }
        serviceSecondLevelService.add(name,serviceFirstId);
        return  Result.success();
    }


    @DeleteMapping("/delete")
    @Log("删除二级服务(逻辑删除)")
    @ApiOperation("删除二级服务(逻辑删除)")
    @ApiImplicitParam("服务id")
    public  Result deleteBySecondId(Integer id){
        log.info("二级服务id:{}",id);
        if (id<1 || id==null) {
            throw new AppException("参数错误");
        }
        serviceSecondLevelService.delById(id);
        return  Result.success();
    }

    @PutMapping("/updateSecondLevel")
    @Log("修改二级服务")
    @ApiOperation("修改二级服务")
    @ApiImplicitParam("修改二级服务表单")
    public Result updateSecondLevel(@Valid @RequestBody UpdateSecondLevelForm updateSecondLevelForm){
        serviceSecondLevelService.updateSecondLevel(updateSecondLevelForm);
        return Result.success();
    }

    @GetMapping("/findServiceFirstList")
    @Cacheable(value = "service",key = "'serviceFirst:'")
    @ApiOperation("加载一级服务列表")
    public Results findServiceSecondList(){
        List<ServiceFirstLevel> list = serviceFirstLevelService.list();
        return Results.instance().setData(list).setMessage("加载一级服务列表成功");
    }

    @GetMapping("/getSecondLevelByFirstId")
    @Log("根据一级服务id查看二级服务（分页）")
    @ApiOperation("根据一级服务id查看二级服务")
    public Result getSecondLevelByFirstId( int serviceFirstId, @RequestParam(defaultValue = "1")int current, @RequestParam(defaultValue = "10")  int size){
        Page<ServiceSecondLevel> secondLevel = serviceSecondLevelService.getSecondLevelByFirstId(serviceFirstId, current, size);
        return  Result.success(secondLevel);
    }
}
